package cn.dnui_lf_705.dnui_16110100705.controller;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_lf_705.dnui_16110100705.Adapter.CommentAdapter;
import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.CollectStatus;
import cn.dnui_lf_705.dnui_16110100705.bean.CommentBean;
import cn.dnui_lf_705.dnui_16110100705.bean.FoodInfo;
import cn.dnui_lf_705.dnui_16110100705.bean.ResultBean;
import cn.dnui_lf_705.dnui_16110100705.listener.OnRetrofitListener;
import cn.dnui_lf_705.dnui_16110100705.model.Constant;
import cn.dnui_lf_705.dnui_16110100705.model.FoodModel;
import cn.dnui_lf_705.dnui_16110100705.model.UserModel;


public class FoodInfoActivity extends BaseActivity implements OnRetrofitListener<Object> {
    private CommentAdapter adapter;
    private FoodModel foodModel;
    FoodInfo foodInfo;
    private String phonenum,food_id,user_id;
    private Context mContext;
    ImageView foodinfo_pic;
    private Button foodinfo_return,foodinfo_purchase;
    private ImageView foodinfo_collect,foodinfo_call;
    private TextView foodinfo_bottom_img,foodinfo_bottom_collect;
    private TextView foodinfo_intro,foodinfo_price,foodinfo_jianjie;
    RecyclerView foodinfo_comments;
    String result;
    SharedPreferences sharedPreferences;
    private EditText pinglun;
    private Button tijiao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_info);
        phonenum = getIntent().getStringExtra("phonenum");
        food_id = getIntent().getStringExtra("id");
        sharedPreferences = getSharedPreferences("yht",Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("user_id","");
        foodinfo_collect = findViewById(R.id.foodinfo_collect);
        foodinfo_call = findViewById(R.id.foodinfo_call);
        foodinfo_purchase = findViewById(R.id.foodinfo_purchase);
        foodinfo_intro = findViewById(R.id.foodinfo_intro);
        foodinfo_price = findViewById(R.id.foodinfo_price);
        foodinfo_jianjie = findViewById(R.id.foodinfo_jianjie);
        foodinfo_bottom_collect = findViewById(R.id.foodinfo_bottom_collect);
        foodinfo_comments = findViewById(R.id.food_info_comment);
        foodinfo_pic = findViewById(R.id.foodinfo_pic);
        tijiao = findViewById(R.id.tijiao);
        foodinfo_comments.setLayoutManager(new LinearLayoutManager(this));
        mContext = getApplicationContext();
        initViews();
        initData();
        initEvents();
        if (setEnableSliding()) {
            ArActSlidLayout rootView = new ArActSlidLayout(this);
            rootView.bindActivity(this);//绑定需要窗口的布局：
        }
    }
    protected boolean setEnableSliding() {//默认返回是需要的，只需继承的时候重写次代码 true : 需要；false :不需要；
        return true;
    }






    //增加【返回】【联系商家】和【立即购买】等按钮处理事件
    @Override
    void initViews() {

//        foodinfo_return.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //finsh();
//            }
//        });
        foodinfo_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });
        foodinfo_purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodInfoActivity.this,PurchaseActivity.class);
                intent.putExtra("shopid",foodInfo.getShop_id());
                intent.putExtra("food_id",foodInfo.getFood_id());
                intent.putExtra("foodname",foodInfo.getFoodname());
                intent.putExtra("price",foodInfo.getPrice());
                startActivity(intent);
            }
        });
        foodinfo_call.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {       //没看懂
                Uri uri = Uri.parse("tel:" + phonenum);
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                if(checkSelfPermission(Manifest.permission.CALL_PHONE)!=
                        PackageManager.PERMISSION_GRANTED){
                    startActivity(intent);
                    return;
                }
            }
        });
    }

    @Override
    void initEvents() {
        //处理【返回】、【收藏】、【立即购买】和【联系商家】按钮的点击事件
        //【返回】按钮调用finish()关闭当前界面
        //【收藏】按钮调用collect()方法
        //【立即购买】按钮打开购买界面
        //【联系商家】按钮打开打电话界面

    }

    @Override
    void initData() {
        //通过Intent获取菜谱id
        //获取菜谱的详细信息，调用showFoodInfo()方法
        showFoodInfo(food_id);
        //获取菜谱的评论列表，调用showComments()方法
        showComments();
        //获取菜谱的收藏状态，调用iscollect()方法
        iscollected();
    }

    //获取菜谱的详细信息
    private void showFoodInfo(String food_id)
    {
        foodModel= new FoodModel();
        foodModel.getFoodById(food_id,this);

    }
    //获取菜谱评论列表
    private void showComments(){
        foodModel = new FoodModel();
        foodModel.getAllCommentsByFood(food_id,this);
    }
    //判断菜谱收藏状态
    private void iscollected(){
        foodModel = new FoodModel();
        foodModel.isCollected(user_id,food_id,"1",this);
    }
    //对菜谱的收藏信息进行修改
    private void collect(){
        UserModel userModel = new UserModel();
        userModel.userCollectFood(user_id,food_id,this);
    }


    @Override
    public void onsuccess(Object o, int flag) {
        switch (flag)
        {
            case 6:     //userCollectFood
                if(((ResultBean)o).getSuccess().equals("1"))
                {
                    if(result.equals("0")){
                        foodinfo_collect.setImageResource(android.R.drawable.btn_star_big_off);
                        foodinfo_bottom_collect.setText("取消收藏");

                        foodinfo_collect.setImageResource(android.R.drawable.btn_star_big_on);
                        showToast("收藏成功");
                        result="1";
                    }
                    else {
                        foodinfo_collect.setImageResource(android.R.drawable.btn_star_big_on);
                        foodinfo_bottom_collect.setText("收藏");

                        foodinfo_collect.setImageResource(android.R.drawable.btn_star_big_off);
                        showToast("取消收藏成功");
                        result = "0";
                    }
                }
                else {
                    showToast("操作失败");
                }
                break;
            case 2:     //isCollected
                result=((CollectStatus)o).getCollected().toString();
                if(result.equals("0")){
                    foodinfo_collect.setImageResource(android.R.drawable.btn_star_big_off);
                    foodinfo_bottom_collect.setText("收藏");
                    foodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.blue));
                }
                else
                {
                    foodinfo_collect.setImageResource(android.R.drawable.btn_star_big_on);
                    foodinfo_bottom_collect.setText("取消收藏");
                    foodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.blue));
                }
                    break;
            case 5:     //getAllCommentsByFood
                adapter = new CommentAdapter(FoodInfoActivity.this,(List<CommentBean>) o,R.layout.comment_item);
                foodinfo_comments.setAdapter(adapter);
                break;
            case 4:     //getfoodbyid
                foodInfo=(FoodInfo)o;
                if(!foodInfo.getPic().equals(""))
                {
                    Picasso.with(this).load(Constant.BASE_URL+foodInfo.getPic()).into(foodinfo_pic);//////
                }
                foodinfo_intro.setText(foodInfo.getFoodname());
                foodinfo_price.setText("￥"+foodInfo.getPrice()+"元");
                foodinfo_jianjie.setText("简介："+foodInfo.getIntro());
                break;
            case 15:

        }
    }

    @Override
    public void onFailure(String string) {
        showToast("获取菜品详情失败");
    }
}
